/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tests;
import entities.tools.FilledForm;
import entities.tools.Form;
import entities.tools.Question;
import entities.users.FormSigner;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.ejb.FilledFormBeanInterface;
import util.ejb.FormBeanInterface;
import util.ejb.FormFillingBeanInterface;
import util.ejb.FormSignerBeanInterface;

/**
 * Servlet used for testing the FillingFormBean.
 *
 * @author Marek
 */
@WebServlet(name = "test3Servlet", urlPatterns = {"/test3"})
public class FillingFormBeanInOneRequestTest extends HttpServlet {

    @PersistenceContext(name = "test", unitName = "JEE-study-warPU")
    private EntityManager em;

    @EJB
    FormBeanInterface formBean;

    @EJB
    FormFillingBeanInterface formFillingBean;

    @EJB
    FormSignerBeanInterface formSignerBean;

    @EJB
    FilledFormBeanInterface filledFormBean;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet FillingFormBeanTest</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet FillingFormBeanTest at " + request.getContextPath() + "</h1>");

        out.println("Remove all forms<br/>");
        List<Form> forms = formBean.getAllForms();
        for (Form form : forms) {
            formBean.forcedDeleteForm(form);
        }
        out.println("Remove all FormSigner<br/>");
        List<FormSigner> formSigners = formSignerBean.getFormSigners();
        for (FormSigner formSigner : formSigners) {
            formSignerBean.deleteFormSigner(formSigner);
        }
        out.println("Remove all FilledForm<br/>");
        List<FilledForm> filledForms = filledFormBean.getFilledForms();
        for (FilledForm filledForm : filledForms) {
            filledFormBean.deleteFilledForm(filledForm);
        }
        out.println("Remove all Answers<br/>");
        out.println("Look if all answers are deleted automatically by the persistance container<br/>");
        out.println("Remove all Questions<br/>");
        out.println("Look if all questions are deleted automatically by the persistance container<br/>");
        for (int x = 0; x < 15; x++) {
            Form form = formBean.createNewForm();
            form.setName("Form " + x);
            out.println("Creating form " + x + "<br/>");
            for (int z = 0; z < x; z++) {
                Question question = formBean.createNewQuestion(form);
                question.setQuestion("Question" + x + ":" + z);
                formBean.updateQuestion(form, question);
                out.println("Creating question " + x + ":" + z + "<br/>");
            }
            formBean.updateForm(form);
        }
        out.println("All needed forms, with questions have been created");

        //Todo FormSigner creating
        FormSigner formSigner = formSignerBean.createFormSigner();

        out.println("Gets all created forms from database<br/>");
        List<Form> createdForms = formBean.getAllForms();
        for (Form createdForm : createdForms) {
            out.println("Filling the form '" + createdForm.getName() + "'<br/>");
            formFillingBean.beginFillingForm(createdForm, formSigner);
            out.println("Gets all questions from the form '" + createdForm.getName() + "'<br/>");
            List<Question> questions = formBean.getAllQuestions(createdForm);
            for (Question question : questions) {
                out.println("Answers the question '" + question.getQuestion() + "'<br/>");
                String answerValue=""; 
                for (int z = 0; z < questions.size(); z++) {
                    answerValue+="Ansfer element:" + z+"¶";
                }
                formFillingBean.answerQuestion(question,answerValue);
                out.println("Question '" + question.getQuestion() + " has been ansfered'<br/>");
            }

            out.println("Form " + createdForm.getName() + " has been sended<br/>");
            formFillingBean.sendForm();
            //TODO: Add check if all questions have been ansfered. If not, form cannot be sended
        }

        out.println("All forms has been answered, and sent.<br/>");

        out.println("The FillingFormBeanInOneRequest test was ended without any noticed problems :-) All looks fine :-)");
            //here goes tests of adding again ansfer to the same question
        //here goes tests of cancelation of the form filling
        //here goes tests of starting filling the form, which don't egist
        //here goes tests of giving ansfers to questions from different form
        out.println("</body>");
        out.println("</html>");

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
